/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  KcTurn
 * 
 * Description of class:
 * 
 * Concrete KarelCommand.
 * 
 */

package karel.gamelogic.commands;

import karel.gamelogic.GameContainer;
import karel.gamelogic.world.Direction;
import karel.gamelogic.world.KarelRobot;
import karel.graphics.Renderer;

public class KcTurn extends KarelCommand {

    public enum Turn {

        LEFT, RIGHT
    }
    private double originalDir;
    private double targetDir;
    /**
     * Special case is either: 
     * Asking to turn Left: 
     *   originalDir: -1.5707963267948966 (NORTH) 
     *   targetDir:    3.141592653589793 (WEST) 
     * or Asking to turn Right: 
     *   originalDir:  3.141592653589793 (WEST) 
     *   targetDir:   -1.5707963267948966 (NORTH)
     */
    private boolean specialCase;
    private double eps = 0.001;
    private double delta;

    public KcTurn(KarelRobot robot, Renderer renderer, Turn turnDir) {
        super(robot,renderer);

        delta = 0.0;
        specialCase = false;

        originalDir = robot.direction;
        if (turnDir == Turn.LEFT) {
            targetDir = Direction.getDir(Direction.turnLeft(robot.directionName));
            if (robot.directionName == Direction.DirName.NORTH) {
                specialCase = true;
                targetDir *= -1;
            }
            robot.directionName = Direction.turnLeft(robot.directionName);
        } else if (turnDir == Turn.RIGHT) {
            targetDir = Direction.getDir(Direction.turnRight(robot.directionName));
            if (robot.directionName == Direction.DirName.WEST) {
                specialCase = true;
                targetDir = originalDir - targetDir;
            }
            robot.directionName = Direction.turnRight(robot.directionName);
        }
    }

    @Override
    public void update() {
        robot.direction = getNextDeltaPos();
        if (Math.abs(robot.direction - targetDir) < eps) {
            if(specialCase) {
                robot.direction = Direction.getDir(robot.directionName);
            } else {
                robot.direction = targetDir;
            }
            commandDone = true;
        }
    }

    @Override
    public Object getCommandReturnValue() {
        return null;
    }

    private double getNextDeltaPos() {
        if(delta == 0.0) {
            delta = (targetDir - originalDir) / GameContainer.gameAnimationSpeed;
        }
        return robot.direction + delta;
    }
}
